<%@ page contentType="image/jpeg" %>
<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.*" %>
<%@ page import="java.util.*"%>
<%@ page import="javax.imageio.*" %>

<%!  
     Color getRandColor(int fc,int bc){
        Random random = new Random();
        if(fc>255) fc=255;
        if(bc>255) bc=255;

        int red=fc+random.nextInt(bc-fc);
        int green=fc+random.nextInt(bc-fc);
        int blue=fc+random.nextInt(bc-fc);
        return new Color(red,green,blue);
     }
     
     String getRandString(){
     
		Random random = new Random();
		int div = 3 - random.nextInt(2);
		char[] chs = new char[6];
		int r;
		for (int i = 0; i < 6; i++) {
			if (i % div == 2) {
				r = random.nextInt(10);
				r += 48;
			} else {
				r = random.nextInt(26);
				if (r % 2 == 1) {
					r += 65;
				} else {
					r += 97;
				}
				
			}
			chs[i] = (char) r;
		}
        return String.valueOf(chs);
     }
%>

<%

    response.setHeader("Pragma","No-cache");
    response.setHeader("Cache-Control","no-cache");
    response.setDateHeader("Expires", 0);

    int width=170, height=50;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    Graphics g = image.getGraphics();

    g.setColor(getRandColor(200,250));
    g.fillRect(0, 0, width, height);
	
	Font[] fonts = new Font[6];
	fonts[0] = new Font("Bodoni MT Black", Font.ITALIC , 30);
	fonts[1] = new Font("Calibri", Font.BOLD , 25);
	fonts[2] = new Font("Harrington", Font.ITALIC , 25);
	fonts[3] = new Font("Imprint MT Shadow", Font.ITALIC , 25);
	fonts[4] = new Font("Cambria Math", Font.PLAIN , 25);
	fonts[5] = new Font("Curlz MT", Font.ITALIC , 30);

    g.setColor(new Color(0,0,0));
    g.drawRect(0,0,width-1,height-1);

	String str = getRandString();
	int posY;
	for (int i = 0; i < str.length(); i++) {
		Random random = new Random();
		int r = random.nextInt(6);
		g.setFont(fonts[r]);
		if (r % 2 == 1) {
			posY = 34;
		} else {
			posY = 26;
		}
		g.drawString(str.substring(i, i + 1) , 10 + 25 * i, posY);
	}

	g.dispose();

	ImageIO.write(image, "JPEG", response.getOutputStream());
%>